package bar.dark.lee.setting.postmoney;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import bar.dark.lee.BDConstents;
import bar.dark.lee.IntroActivitiy;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.common.DBSQLite;
import bar.dark.lee.setting.font.FontActivity;

public class MonthPostMoney extends SuperActivity
{
	private DBSQLite sql;
	
	private View startDateView;
	private View termView;
	
	private RadioButton startRadioButton;
	private RadioButton termRadioButton;
	
	private Button saveButton;
	
	private SharedPreferences pref = null;
	private int checkFlag = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		sql = DBSQLite.getInstance(this);
		
		setContentView(R.layout.setting_month_postmoney);
		
		setTitle("이달의 카드대금 설정");
		setGoneTipButton();
		
		setFindView();
		setOnClickListener();
		new ShowDialog().execute(BDUtil.asyncFlag, 0);
	}
	
	private void setFindView()
	{
		startDateView = findViewById(R.id.settingPostMoneyStartDateView);
		termView = findViewById(R.id.settingPostMoneyTermView);
		
		startRadioButton = (RadioButton) findViewById(R.id.settingPostMoneyStartDateCheckBox);
		termRadioButton = (RadioButton) findViewById(R.id.settingPostMoneyTermCheckBox);
		
		saveButton = (Button) findViewById(R.id.settingPostMonetSaveButton);
	}

	private void setOnClickListener()
	{
		startDateView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				startRadioButton.setChecked(true);
				termRadioButton.setChecked(false);
			}
		});
		
		termView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				startRadioButton.setChecked(false);
				termRadioButton.setChecked(true);
			}
		});
		
		saveButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				new ShowDialog().execute(BDUtil.asyncFlag, 1);
			}
		});
	}
	
	private void setData()
	{
		pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		checkFlag = pref.getInt(BDConstents.MONTH_POSTMONEY_SETTING, 0);
	}
	
	private void save()
	{
		SharedPreferences.Editor editor = pref.edit();
		
		if (startRadioButton.isChecked())
		{
			editor.putInt(BDConstents.MONTH_POSTMONEY_SETTING, 0);
		}
		else
		{
			editor.putInt(BDConstents.MONTH_POSTMONEY_SETTING, 1);
		}
		
		editor.commit();
	}
	
	/**
	 * 다이얼로그 뛰우기 
	 */
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
			if (BDUtil.btnEnable)
			{
				BDUtil.btnEnable = false;
				BDUtil.asyncFlag = -1;
				
				dialog = new ProgressDialog(MonthPostMoney.this);
				dialog.setCancelable(false);
				dialog.setMessage("Loading...");
				dialog.show();
			}
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) {
			if (arg0[0] == 0)
			{
				if (arg0[1] == 0)
				{	// 초기셋팅
					setData();
					return 0;
				}
				else if (arg0[1] == 1)
				{	// 폰트 저장
					save();
					return 1;
				}
			}
			
			return null;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			
			dialog.dismiss();
			
			if (result == 0)
			{
				BDUtil.btnEnable = true;
				BDUtil.asyncFlag = 0;
				
				if (checkFlag == 0)
				{
					startRadioButton.setChecked(true);
					termRadioButton.setChecked(false);
				}
				else if (checkFlag == 1)
				{
					termRadioButton.setChecked(true);
					startRadioButton.setChecked(false);
				}
			}
			else if (result == 1)
			{	// 폰트 저장
				BDUtil.showDialogOK(MonthPostMoney.this, "알림", "이달의 카드 설정이 변경되었습니다. 설정 적용을 위해 초기 화면으로 이동됩니다.", new DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						BDUtil.btnEnable = true;
						BDUtil.asyncFlag = 0;
						
						Intent intent = new Intent(superContext, IntroActivitiy.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
						startActivity(intent);
					}
				});
			}
		}
	}

	@Override
	protected void tipButton()
	{
	}
}
